import React,{Component} from 'react'
import { Input } from 'antd';
import './login.css'
import {HttpUtil} from "bdjf_http";
import API from '../../http/api';
import { Button } from 'antd';
import { Link  } from "react-router-dom";
import axios from "axios"


class Login extends Component{

    state = {
        userName:'',
        password:'',
        loading:false
    }

  constructor(props) {
    super(props);
  }




  render() {
    return (
      <div className="col-center">
          <div className="col form">
            <div>
              <span className="title">登录</span>
              <Link replace to="/reg/">去注册</Link>
            </div>
              
              <span className="input_label">用户名:</span>
              <Input allowClear  onChange={this.dataChange.bind(this,'userName')} value={this.state.username} placeholder="请输入用户名" />
              
              <span className="input_label">密码:</span>
              <Input allowClear  onChange={this.dataChange.bind(this,'password')} value={this.state.password} placeholder="请输入密码" />
              
              <Button className="margin_item" type="primary" loading={this.state.loading} onClick={this.login.bind(this)}>
              登录
              </Button>

              <Button className="margin_item" type="primary" onClick={this.test.bind(this)}>
              test
              </Button>
          </div>
      </div>
    )
  }

  dataChange(key,e){
    this.setState({
        [key]:e.target.value
    })
  }

  login(){
    const params = {
      ...this.state
    }
    console.log(params)
    this.setState({
        loading:true
    })
    HttpUtil.request(API.login,params)
    .then(res=>{
        console.log(res)
        this.setState({
            loading:false
        })
        const httpUtil = new HttpUtil()

        const axiosInstance = axios.create({
            baseURL: 'http://localhost:8088',
            timeout: 5000,
            headers: {
              'Content-Type': 'application/json',
              'token':res.data.token
            }
        });

        httpUtil.setAxios(axiosInstance)
    }).catch(err=>{
        console.log(err)
        this.setState({
            loading:false
        })
    })
  }

  test(){
    HttpUtil.request(API.userInfo)
    .then(res=>{
        console.log(res);
    })
  }

}

export default Login;
